package com.ldc.wandroid.ui.activitys;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import androidx.annotation.IdRes;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;

import com.ldc.common.common.CommonViewClick;
import com.ldc.common.mvvm.BaseMvvmActivity;
import com.ldc.common.utils.ViewModelUtils;
import com.ldc.common.utils.ViewUtils;
import com.ldc.wandroid.R;
import com.ldc.wandroid.databinding.ActivityMainBinding;
import com.ldc.wandroid.ui.fragments.HomeFragment;
import com.ldc.wandroid.ui.fragments.MeFragment;
import com.ldc.wandroid.ui.fragments.ProjectTabFragment;
import com.ldc.wandroid.ui.fragments.SquareFragment;
import com.ldc.wandroid.ui.fragments.SystemFragment;
import com.ldc.wandroid.ui.fragments.WeChatNumberFragment;
import com.ldc.wandroid.viewmodel.MainViewModel;

public class MainActivity extends BaseMvvmActivity<ActivityMainBinding, MainViewModel> {

    private Fragment indexFragment = new Fragment();
    private HomeFragment homeFragment;
    private SystemFragment systemFragment;
    private ProjectTabFragment projectTabFragment;
    private SquareFragment squareFragment;
    private MeFragment meFragment;
    private WeChatNumberFragment weChatFragment;

    public static void start(Context context) {
        context.startActivity(new Intent(context, MainActivity.class));
    }


    @Override
    protected ActivityMainBinding initViewBinding() {
        return ActivityMainBinding.inflate(getLayoutInflater());
    }

    @Override
    protected void main(Bundle savedInstanceState) {
        bottomClick();
        // 默认显示项目
        changeIcon(R.id.tv_home);
        changeFragment(getFragment(R.id.fl_home));
    }


    @Override
    protected MainViewModel initViewModel() {
        return ViewModelUtils.getViewModel(this, MainViewModel.class);
    }


    private void bottomClick() {
        CommonViewClick<View> bottomViewListener = view -> {
            Fragment fragment = getFragment(view.getId());
            if (null == fragment) {
                return;
            }
            changeFragment(fragment);
        };
        ViewUtils.addViewClick(this, R.id.fl_home, bottomViewListener);
        ViewUtils.addViewClick(this, R.id.fl_sys, bottomViewListener);
        ViewUtils.addViewClick(this, R.id.fl_project, bottomViewListener);
        ViewUtils.addViewClick(this, R.id.fl_square, bottomViewListener);
        ViewUtils.addViewClick(this, R.id.fl_wechat, bottomViewListener);
        ViewUtils.addViewClick(this, R.id.fl_me, bottomViewListener);
    }

    /**
     * 钱换页面
     */
    private void changeFragment(Fragment fragment) {
        if (null == fragment) {
            return;
        }
        if (fragment != indexFragment) {
            FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
            if (fragment.isAdded()) {
                transaction.show(fragment).hide(indexFragment);
            } else {
                transaction.add(R.id.fragment_container, fragment)
                        .show(fragment)
                        .hide(indexFragment);
            }
            transaction.commit();
        }
        indexFragment = fragment;
    }

    /**
     * 修改icon
     *
     * @param id id
     */
    private void changeIcon(@IdRes int id) {
        viewBinding.ivHome.setImageResource(id == R.id.tv_home ? R.drawable.icon_home_pre : R.drawable.icon_home);
        viewBinding.tvHome.setTextColor(id == R.id.tv_home ? getResources().getColor(R.color.color_font_select) : getResources().getColor(R.color.color_font_main));
        viewBinding.tvHome.setTextSize(id == R.id.tv_home ? 14 : 13);
        viewBinding.ivSys.setImageResource(id == R.id.tv_sys ? R.drawable.icon_system_pre : R.drawable.icon_system);
        viewBinding.tvSys.setTextColor(id == R.id.tv_sys ? getResources().getColor(R.color.color_font_select) : getResources().getColor(R.color.color_font_main));
        viewBinding.tvSys.setTextSize(id == R.id.tv_sys ? 14 : 13);
        viewBinding.ivProject.setImageResource(id == R.id.tv_project ? R.drawable.icon_project_pre : R.drawable.icon_project);
        viewBinding.tvProject.setTextColor(id == R.id.tv_project ? getResources().getColor(R.color.color_font_select) : getResources().getColor(R.color.color_font_main));
        viewBinding.tvProject.setTextSize(id == R.id.tv_project ? 14 : 13);
        viewBinding.ivSquare.setImageResource(id == R.id.tv_square ? R.drawable.icon_squera_pre : R.drawable.icon_squera);
        viewBinding.tvSquare.setTextColor(id == R.id.tv_square ? getResources().getColor(R.color.color_font_select) : getResources().getColor(R.color.color_font_main));
        viewBinding.tvSquare.setTextSize(id == R.id.tv_square ? 14 : 13);
        viewBinding.ivWechat.setImageResource(id == R.id.tv_wechat ? R.drawable.icon_wechat_pre : R.drawable.icon_wechat);
        viewBinding.tvWechat.setTextColor(id == R.id.tv_wechat ? getResources().getColor(R.color.color_font_select) : getResources().getColor(R.color.color_font_main));
        viewBinding.tvWechat.setTextSize(id == R.id.tv_wechat ? 14 : 13);
        viewBinding.ivMe.setImageResource(id == R.id.tv_me ? R.drawable.icon_me_pre : R.drawable.icon_me);
        viewBinding.tvMe.setTextColor(id == R.id.tv_me ? getResources().getColor(R.color.color_font_select) : getResources().getColor(R.color.color_font_main));
        viewBinding.tvMe.setTextSize(id == R.id.tv_me ? 14 : 13);
    }

    /**
     * 获取fragment
     *
     * @param index id
     * @return Fragment
     */
    @SuppressLint("NonConstantResourceId")
    private Fragment getFragment(@IdRes int index) {
        switch (index) {
            case R.id.fl_home:
                changeIcon(R.id.tv_home);
                if (homeFragment == null) {
                    homeFragment = HomeFragment.newInstance();
                }
                return homeFragment;
            case R.id.fl_sys:
                changeIcon(R.id.tv_sys);
                if (systemFragment == null) {
                    systemFragment = SystemFragment.newInstance();
                }
                return systemFragment;
            case R.id.fl_project:
                changeIcon(R.id.tv_project);
                if (projectTabFragment == null) {
                    projectTabFragment = ProjectTabFragment.newInstance();
                }
                return projectTabFragment;
            case R.id.fl_square:
                changeIcon(R.id.tv_square);
                if (squareFragment == null) {
                    squareFragment = SquareFragment.newInstance();
                }
                return squareFragment;
            case R.id.fl_wechat:
                changeIcon(R.id.tv_wechat);
                if (weChatFragment == null) {
                    weChatFragment = WeChatNumberFragment.newInstance();
                }
                return weChatFragment;
            case R.id.fl_me:
                changeIcon(R.id.tv_me);
                if (meFragment == null) {
                    meFragment = MeFragment.newInstance();
                }
                return meFragment;
            default:
                return null;
        }
    }
}